Skip to main content
OpenCLIP supports multiple tokenization strategies to handle different text encoders and languages.

Quick Start

get_tokenizer()

Automatically selects the appropriate tokenizer based on model configuration:
str
required
Model identifier. Can use schemas:
  • Built-in: 'ViT-B-32'
  • HuggingFace: 'hf-hub:org/repo'
  • Local: 'local-dir:/path/to/model'
int
Maximum sequence length. Defaults to model’s configured length (usually 77).
str
Cache directory for downloading HuggingFace tokenizers

Tokenizer Types

SimpleTokenizer

Default BPE tokenizer used by most CLIP models:
int
default:"77"
Maximum sequence length (including special tokens)
str
default:"lower"
Text preprocessing:
  • 'lower': Lowercase + whitespace cleaning
  • 'whitespace': Whitespace cleaning only
  • 'canonicalize': Remove punctuation + lowercase
str
Token reduction strategy when exceeding context length:
  • 'simple': Random contiguous block
  • 'random': Random tokens (preserve order)
  • 'shuffle': Random tokens (shuffle)
  • 'syntax': Priority based on POS tags

HFTokenizer

HuggingFace Transformers tokenizer wrapper for models using pretrained LMs:
str
required
HuggingFace tokenizer identifier (e.g., ‘roberta-base’, ‘xlm-roberta-large’)
int
default:"77"
Maximum sequence length
str
default:"whitespace"
Text cleaning mode
bool
default:"False"
Remove separator tokens from output
str
Language code for multilingual tokenizers (e.g., ‘en’, ‘fr’, ‘de’)

SigLipTokenizer

SentencePiece tokenizer for SigLIP models:
Variants:
  • 'c4-en': English only (vocab_size=32,000)
  • 'mc4': Multilingual (vocab_size=250,000)
  • 'gemma': SigLIP2 models (vocab_size=256,000)

Context Length

Default Context Lengths

Different models use different context lengths:
  • CLIP models: 77 tokens
  • SigLIP models: 64 tokens
  • CoCa models: 76 tokens (+ 1 for generation)

Handling Long Text

Custom Context Length

Changing context length requires model weights trained with that length. For pretrained models, use the original context length.

Text Preprocessing

Cleaning Modes

Special Tokens

SimpleTokenizer uses special tokens:
  • <start_of_text> (token_id: 49406)
  • <end_of_text> (token_id: 49407)

Multilingual Tokenization

XLM-RoBERTa Models

SigLIP Multilingual

Advanced Usage

Batch Tokenization

Custom Vocabulary

Decoding Tokens

Complete Example

The tokenizer is automatically selected based on model configuration. For most CLIP models, this will be SimpleTokenizer. Models using HuggingFace text encoders will use HFTokenizer.